package com.zaoniao.enstudy.api.controller;

import com.zaoniao.enstudy.domain.service.FileService;
import com.zaoniao.enstudy.infracore.web.DataResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件相关Api：
 * 通过excel导入数据
 */
@RestController
@Tag(name = "Excel导入相关api")
@RequestMapping("/excel")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class FileController {
    private final FileService fileService;

    /**
     * 单词批量导入.
     *
     * @param file 文件
     * @return -
     */
    @PostMapping("/import")
    @Operation(summary = "Excel批量导入")
    public DataResponse<String> importExcel(@RequestParam MultipartFile file) {
        try {
            fileService.importExcel(file);
        } catch (IOException e) {
            return DataResponse.errorMap(e.getMessage());
        }
        return DataResponse.ok("导入成功！");
    }
}
